﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyAlbum.Logging;
using System.IO;
using System.Threading;
using MyAlbum.Services;

namespace PersonalAlbum
{
    public class WebLogger
    {
        private static ILogger logger;
        public static string LoggingErrors = "logs.txt";
        private static ReaderWriterLock readerWriterLock;
        private static bool enableTrace;//the value will stay till application restart
        #region ILogger Members
        static WebLogger()
        {
            readerWriterLock = new ReaderWriterLock();
            var iconfig = ObjectFactory.GetInstance<IConfiguration>();
            enableTrace = iconfig.EnableTrace;
            logger = ObjectFactory.GetInstance<ILogger>();
            logger.Email = iconfig.AdminEmail;
            logger.SmtpHost = iconfig.MailHost;
            logger.SmtpPassword = iconfig.MailPassword;
            logger.SmtpUsername = iconfig.MailUserName;
            logger.SenderEmail = iconfig.MailFromAddress;
            logger.EnableTrace = iconfig.EnableTrace;
            logger.EmailOnErrors = iconfig.EmailOnErrors;
        }
        public static void LogError(Exception e1)
        {
            LogError(e1, null);
        }


        public void LogError(string message)
        {
            LogError(null, message);
        }

        public static void WriteToFile(string message)
        {
            try
            {
                readerWriterLock.AcquireWriterLock(2000);
                File.AppendAllText(HttpContext.Current.Server.MapPath("~/app_Data/" + LoggingErrors), DateTime.Now.ToString() + message + "\r\n");
            }
            catch { }
            finally
            {
                readerWriterLock.ReleaseLock();
            }
        }
        public static string GetContextData()
        {
            return "URL: "+HttpContext.Current.Request.RawUrl + "\r\nForm Data: " + HttpContext.Current.Request.Form + "\r\n";
        }
        public static void LogError(Exception e1, string message)
        {
            try
            {
                logger.LogError(e1, GetContextData() + message);
            }
            catch (Exception e2)
            {
                WriteToFile(e2);
            }

        }

        private static void WriteToFile(Exception e1, string message)
        {
            message = GetContextData() + "error:" + message;
            if (e1 != null)
            {
                message += e1.Message + " " + e1.StackTrace;
            }
            WriteToFile(message);
        }

        public static void Trace(object data)
        {
            if (enableTrace)
            {
                try
                {
                    logger.Trace(GetContextData() + data);
                }
                catch (Exception e2)
                {
                    WriteToFile(e2);
                }
            }
        }

        public static void WriteToFile(object data)
        {
            string message = null;
            if (data != null)
            {
                message = data.ToString();
            }
            WriteToFile(message);
        }

        public static void Info(string message)
        {
            if (enableTrace)
            {
                try
                {
                    logger.Info(GetContextData() + message);
                }
                catch (Exception e2)
                {
                    WriteToFile(e2);
                }
            }
        }

        public static void Warning(string message)
        {
            try
            {
                logger.Warning(GetContextData() + message);
            }
            catch (Exception e2)
            {
                WriteToFile(e2);
            }
        }

        #endregion
    }
}
